﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace ForExam
{
    public partial class ShowTopic : Form
    {
        #region 界面设置
        #region 无边框窗体拖动
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        private void MainForm_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
        #endregion

        public ShowTopic(string strQuestionPath,string strCollectPath = "")
        {
            InitializeComponent();
            m_QuestionPath = strQuestionPath;
        }

        //关闭窗口
        private void tslClose_Click(object sender, EventArgs e)
        {
            Close();
        }
        //最小化
        private void tslMin_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        //去掉toolstrip的白边
        private void toolStrip1_Paint(object sender, PaintEventArgs e)
        {
            if ((sender as ToolStrip).RenderMode != ToolStripRenderMode.System)
                return;
            Rectangle rect = new Rectangle(0, 0, this.toolStrip1.Width - 2, this.toolStrip1.Height - 8);
            e.Graphics.SetClip(rect);
        }
        #endregion

        public const int NumberOfA = 65;
        public string m_strDetailModeOpen = "开启详解模式";
        public string m_strDetailModeClose = "关闭详解模式";
        bool m_bOpenDetailMode = false;
        string m_QuestionPath;
        int m_Index = 0;
        List<Topic> m_AllTopic = new List<Topic>();
        Topic m_currentTopic;

        private void ShowTopic_Load(object sender, EventArgs e)
        {
            //m_QuestionPath = @"..\..\Data\SIPMEquestion.xml";
            if (!System.IO.File.Exists(m_QuestionPath))
            {
                CommondMethod.OutLog("文件缺失");
                return;
            }
            //加载题目
            LoadTopics();
            //显示第i题
            m_Index = 0;
            ShowTopicByIndex(m_Index);
        }

        private void ShowTopicByIndex(int pIndex)
        {
            if (pIndex < 0 || pIndex >= m_AllTopic.Count)
                return;
            m_currentTopic = m_AllTopic[m_Index];
            lblTopicContent.Text = m_currentTopic.Content;
            panelCandidates.Controls.Clear();
            foreach (Candidate item in m_currentTopic.CandidateItems)
            {
                RadioButton pRadioButn = new RadioButton()
                {
                    Checked = false,
                    Dock = DockStyle.Top,
                    Text = item.Name + "." + item.Content,
                };
                pRadioButn.CheckedChanged += pRadioButn_CheckedChanged;
                panelCandidates.Controls.Add(pRadioButn);
            }
        }

        //选择结果之后触发的事件
        void pRadioButn_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton pRadioButn = (RadioButton)sender;
            int iChooseNum = pRadioButn.Text[0] - NumberOfA;//(A,B,C,D) - A
            int iResultNum =  Convert.ToInt32(m_currentTopic.Result);
            bool bChooseRight = (iResultNum == iChooseNum);
            #region 显示结果，正确答案字体为绿色，如果选错，错误项字体为红色
            //Controls 是按 D,C,B,A顺序来的
            panelCandidates.Controls[panelCandidates.Controls.Count-1 -iResultNum ].ForeColor = Color.Green;
            //如果选错了，选错的项按红色显示
            if (!bChooseRight)
                panelCandidates.Controls[panelCandidates.Controls.Count - 1 - iChooseNum].ForeColor = Color.Red;
            #endregion

            //选错了自动出现详解,切到下一题会清空详细解释
            if ((!bChooseRight && chbAutoDetail.Checked)||m_bOpenDetailMode)
                lblDetail.Text = m_currentTopic.Detail;
            //选对了跳到下一题
            if (bChooseRight && chbAutoNext.Checked)
            {
                //停顿1秒
                Thread.Sleep(1000);
                ChangeToNext();
            }

        }
        //加载题目
        private void LoadTopics()
        {
            XmlDocument pDoc = new XmlDocument();
            pDoc.Load(m_QuestionPath);
            XmlNode pRootNode = pDoc.SelectSingleNode("TopicsCollection");
            for (int i = 0; i < pRootNode.ChildNodes.Count; i++)
            {
                XmlElement pXmlEle = pRootNode.ChildNodes[i] as XmlElement;
                Topic pTopic = new Topic();
                pTopic.Identifies = pXmlEle.GetAttribute("Identifies");
                pTopic.ContentType = pXmlEle.GetElementsByTagName("ContentType").Item(0).InnerText;
                pTopic.Content = pXmlEle.GetElementsByTagName("Content").Item(0).InnerText;
                pTopic.Result = pXmlEle.GetElementsByTagName("Result").Item(0).InnerText;
                pTopic.Detail = pXmlEle.GetElementsByTagName("Detail").Item(0).InnerText;

                pTopic.CandidateItems = new List<Candidate>();
                XmlNode ppNode = pXmlEle.SelectNodes("//Candidates")[i];
                for (int j = ppNode.ChildNodes.Count - 1;j>=0 ; j--)
                {
                    Candidate pCandidate = new Candidate();
                    pCandidate.Name = (char)(NumberOfA + j);
                    pCandidate.Content = ppNode.ChildNodes[j].InnerText;
                    pTopic.CandidateItems.Add(pCandidate);
                }

                m_AllTopic.Add(pTopic);
            }
        }
        //上一题
        private void btnPrevious_Click(object sender, EventArgs e)
        {
            ChangeToPrevious();
        }

        //下一题
        private void btnNext_Click(object sender, EventArgs e)
        {
            ChangeToNext();
        }

        private void ChangeToPrevious()
        {
            if (m_Index <= 0)
                return;
            ShowTopicByIndex(--m_Index);
            lblDetail.Text = "";
        }

        private void ChangeToNext()
        {
            if (m_Index >= m_AllTopic.Count - 1)
                return;
            ShowTopicByIndex(++m_Index);
            lblDetail.Text = "";
        }

        private void btnDetailMode_Click(object sender, EventArgs e)
        {
            m_bOpenDetailMode = !m_bOpenDetailMode;
            btnDetailMode.Text = m_bOpenDetailMode ? m_strDetailModeClose : m_strDetailModeOpen;
        }
    }
}
